﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FreeImageAPI;

namespace TLC
{
    public partial class TLC : Form
    {
        public TLC()
        {
            InitializeComponent();

            this._imageWnd = new ImageWnd();
            this._imageWnd.TopLevel = false;
            this._imageWnd.Parent = this;
            this._imageWnd.Show();

            this._imageWnd.SetScale(2, 0.2);
        }

        private void _bnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.Filter = "BMP|*.bmp";

            fd.ShowDialog();

            if (this.init_FI(fd.FileName))
            {
                this._imageWnd.SetFIBitmap(this._bitmap);
            }
        }

        private void TLC_Paint(object sender, PaintEventArgs e)
        {
        }

        private bool init_FI(string filename)
        {
            if (!this._bitmap.IsNull)
            {
                return false;
            }

            FreeImageAPI.FREE_IMAGE_FORMAT fif = FreeImageAPI.FREE_IMAGE_FORMAT.FIF_UNKNOWN;

            fif = FreeImageAPI.FreeImage.GetFileType(filename, 0);

            if (fif == FreeImageAPI.FREE_IMAGE_FORMAT.FIF_UNKNOWN)
            {
                fif = FreeImageAPI.FreeImage.GetFIFFromFilename(filename);
            }

            if ((fif != FreeImageAPI.FREE_IMAGE_FORMAT.FIF_UNKNOWN) 
                && FreeImageAPI.FreeImage.FIFSupportsReading(fif))
            {
                this._bitmap = FreeImageAPI.FreeImage.Load(fif, filename, 0);
            }

            return !this._bitmap.IsNull;
        }

        private
            FreeImageAPI.FIBITMAP _bitmap;
        ImageWnd _imageWnd;
    }
}